package com.zbiti.anvil.basic.admin.gateway.common.controller;

import com.zbiti.framework.security.api.bean.AnvilRole;
import com.zbiti.module.system.base.api.domain.SysMenuDomain;
import com.zbiti.module.system.base.api.domain.SysRoleDomain;
import com.zbiti.module.system.base.api.domain.SysUserDomain;
import com.zbiti.module.system.base.api.service.ISysMenuService;
import com.zbiti.module.system.base.api.service.ISysRoleService;
import com.zbiti.sdk.common.annotation.Log;
import com.zbiti.sdk.common.domain.RestDomain;
import com.zbiti.sdk.common.domain.RestMapDomain;
import com.zbiti.sdk.common.domain.RouterDomain;
import com.zbiti.sdk.common.enums.BusinessType;
import com.zbiti.sdk.common.enums.OperatorType;
import com.zbiti.sdk.common.utils.CollectionUtils;
import com.zbiti.sdk.common.utils.SessionProxyUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 全局信息Controller
 *
 * @author Fanyc
 * @date 2020-11-09 10:46
 */
@Tag(name = "全局信息", description = "获取全局信息的接口")
@RestController
public class GlobalInfoController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @PostMapping("getInfo")
    @Operation(summary = "获取用户信息", description = "获取当前登录的用户详细信息")
    @Log(title = "",businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
    public RestMapDomain getInfo() {
        SysUserDomain loginUser = SessionProxyUtils.getUserInfo(SysUserDomain.class);
        List<SysRoleDomain> sysRoleDomains = sysRoleService.selectByEntity(new SysRoleDomain());
        System.out.println(sysRoleDomains);
        // 角色集合
        Set<String> roles = sysRoleService.getRolePermission(loginUser);
        // 权限集合
        Set<String> permissions = menuService.getMenuPermission(loginUser);
        RestMapDomain result = RestMapDomain.success("user", loginUser);
        // 角色类型
        Set<String> roleTypes = new HashSet<>(3);
        if (CollectionUtils.isNotEmpty(SessionProxyUtils.getSuperRoles(AnvilRole.class))) {
            roleTypes.add("super");
        }
        if (CollectionUtils.isNotEmpty(SessionProxyUtils.getConfigRoles(AnvilRole.class))) {
            roleTypes.add("conf");
        }
        if (CollectionUtils.isNotEmpty(SessionProxyUtils.getApplicationRoles(AnvilRole.class))) {
            roleTypes.add("app");
        }
        if (CollectionUtils.isNotEmpty(SessionProxyUtils.getCustom1Roles(AnvilRole.class))) {
            roleTypes.add("custom1");
        }
        if (CollectionUtils.isNotEmpty(SessionProxyUtils.getCustom2Roles(AnvilRole.class))) {
            roleTypes.add("custom2");
        }
        if (CollectionUtils.isNotEmpty(SessionProxyUtils.getCustom3Roles(AnvilRole.class))) {
            roleTypes.add("custom3");
        }

        result.put("user", loginUser)
                .put("roles", roles)
                .put("roleTypes", roleTypes)
                .put("permissions", permissions);
        return result;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @PostMapping("getRouters")
    @Operation(summary = "获取菜单信息", description = "获取当前登录人所拥有角色权限的菜单")
    public RestDomain<List<RouterDomain>> getRouters() {
        // 用户信息
        SysUserDomain user = SessionProxyUtils.getUserInfo(SysUserDomain.class);
        List<SysMenuDomain> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return RestDomain.success(menuService.buildMenus(menus));
    }
}
